#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>

void send_message(int sock,const char *message);


int main()
{
	int fd = 0;
	int ret = 0;
	struct sockaddr_in  addr;

	memset(&addr,0,sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = 6868;
	addr.sin_addr.s_addr = inet_addr("192.168.0.152");

	fd = socket(AF_INET,SOCK_STREAM,0);
	if(fd < 0)
	{
		perror("socket");
		exit(-1);
	}

	ret = connect(fd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret != 0)
	{
		perror("connect");
		close(fd);
		exit(-1);
	}

	char buf[1024] = {0};
	int i = 0;

	while(1)
	{
		if(i % 2 == 0)
		{
			strcpy(buf,"how are you!");
		}else
		{
			strcpy(buf,"are you ok?");
		}
		
		send_message(fd,buf);

		i = i + 1;
		if(i > 5000) i = 0;
	}

	close(fd);
	return 0;
}

void send_message(int sock,const char *message)
{
	int length = strlen(message);
	int ret = 0;
	char *buf = (char *)malloc(length + 4);

	memcpy(buf,&length,4);
	memcpy(buf + 4,message,length);
	ret = send(sock,buf,length + 4,0);
	if(ret <= 0)
	{
		perror("send");
		exit(-1);
	}
}






